package GUI;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 * The Class JTextFieldLimit.
 * This class made for limiting the text fields size in the project
 *  
 */
public class JTextFieldLimit extends PlainDocument {
	  
  	/** The limit. */
  	private int limit;
	  
  	/**
  	 * Instantiates a new JTextField limiter.
  	 *
  	 * @param limit the limit
  	 */
  	JTextFieldLimit(int limit) {
	    super();
	    this.limit = limit;
	  }

	  /**
  	 * Instantiates a new JTextField limiter.
  	 *
  	 * @param limit the limit
  	 * @param upper the upper
  	 */
  	JTextFieldLimit(int limit, boolean upper) {
	    super();
	    this.limit = limit;
	  }

	  /* (non-Javadoc)
  	 * @see javax.swing.text.PlainDocument#insertString(int, java.lang.String, javax.swing.text.AttributeSet)
  	 */
  	public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
	    if (str == null)
	      return;

	    if ((getLength() + str.length()) <= limit) {
	      super.insertString(offset, str, attr);
	    }
	  }
	}
